package zhongbo.java.air.airmonitor.service.impl;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import zhongbo.java.air.airmonitor.mapper.FlightActivityDailyMapper;
import zhongbo.java.air.airmonitor.model.FlightActivityDaily;
import zhongbo.java.air.airmonitor.service.IFlightActivityDailyService;
import zhongbo.java.air.common.vo.PageVo;
import zhongbo.java.air.dictionary.model.AirAirCraft;


import java.util.List;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年04月08日 星期二 04:07:41
 */
@Service
@Slf4j
public class FlightActivityDailyServiceImpl implements IFlightActivityDailyService<FlightActivityDaily> {

    @Autowired
    private FlightActivityDailyMapper flightActivityDailyMapper;

    @Override
    public List<FlightActivityDaily> selectDailyStats(String date) {
        return flightActivityDailyMapper.selectDailyStats(date);
    }

    @Override
    public List<FlightActivityDaily> getDailyStats(String date) {
        if (StrUtil.isBlank(date)) {
            throw new RuntimeException("data参数必须填写!示例: 2025-04-08");
        }
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("date", date);
        List<FlightActivityDaily> flightActivityDailies = flightActivityDailyMapper.selectList(queryWrapper);
        if (flightActivityDailies.size() == 0) {
            throw new RuntimeException("查询失败 ->>> date：" + date);
        }
        // todo 根据 date 条件, 从 air_flight_activity_daily 表里查询结果,并返回
        return flightActivityDailies;
    }

    @Override
    public FlightActivityDaily insert(FlightActivityDaily flightActivityDaily) {
        int insert = flightActivityDailyMapper.insert(flightActivityDaily);
        if (insert <= 0){
            throw new RuntimeException("添加失败 ->>> " + flightActivityDaily);
        }
        return flightActivityDaily;
    }

    @Override
    public FlightActivityDaily update(FlightActivityDaily flightActivityDaily) {
        int i = flightActivityDailyMapper.updateById(flightActivityDaily);
        if (i <= 0){
            throw new RuntimeException("修改失败 ->>> " + flightActivityDaily);
        }
        return flightActivityDaily;
    }

    @Override
    public String deleteById(String id) {
        int i = flightActivityDailyMapper.deleteById(id);
        if (i <= 0){
            throw new RuntimeException("删除失败 ->>> id：" + id);
        }
        return "删除成功";
    }

    @Override
    public FlightActivityDaily queryById(String id) {
        FlightActivityDaily flightActivityDaily = flightActivityDailyMapper.selectById(id);
        if (flightActivityDaily == null){
            throw new RuntimeException("查询失败 ->>> id：" + id);
        }
        return flightActivityDaily;
    }

    @Override
    public FlightActivityDaily queryByIcaoCodeAndDate(String icaoCode, String date) throws RuntimeException{
        if (StrUtil.isBlank(icaoCode) || date == null) {
            throw new RuntimeException("icaoCode和date参数必须填写!示例: 7800A1, 2025-04-08");
        }
        DateTime parse = DateUtil.parse(date);
        try {
            FlightActivityDaily flightActivityDaily = flightActivityDailyMapper.queryByIcaoCodeAndDate(icaoCode, parse);
            return flightActivityDaily;
        }catch (RuntimeException e){
            throw new RuntimeException("查询失败 ->>> icaoCode：" + icaoCode + ", date：" + date);
        }
    }

    @Override
    public FlightActivityDaily queryByIcao(String icaoCode) throws RuntimeException {
        try {
            FlightActivityDaily flightActivityDaily = flightActivityDailyMapper.queryByIcao(icaoCode);
            return flightActivityDaily;
        }catch (RuntimeException r){
            throw new RuntimeException("查询失败 ->>> icaoCode：" + icaoCode);
        }
    }

    @Override
    public PageVo<FlightActivityDaily> findPage(Integer pageNumber, Integer pageSize
            , String icao, String startDate, String endDate, String keyword
            , String fields, String oderByField, Boolean orderByAsc) {
        QueryWrapper<FlightActivityDaily> queryWrapper  = new QueryWrapper<>();
        //支持指定字段模糊搜索keyword
        if (StrUtil.isNotBlank(keyword) && StrUtil.isNotBlank(fields)) {
            String[] fieldsArray = fields.split(",");
            for (String field : fieldsArray) {
                queryWrapper.or().like(StrUtil.toUnderlineCase(field), keyword);
            }
        }
        //支持指定字段排序
        if (StrUtil.isNotBlank(oderByField) && orderByAsc){
            queryWrapper.orderByAsc(oderByField);
        }
        IPage<FlightActivityDaily> pageParam = new Page<>(pageNumber,pageSize);
        IPage<FlightActivityDaily> flightActivityDailyIPage = flightActivityDailyMapper.selectPage(pageParam, queryWrapper);
        return new PageVo<>(flightActivityDailyIPage);
    }
}
